from tensorflow.python.ops.gen_experimental_dataset_ops import max_intra_op_parallelism_dataset


class Solution:
    def findMedianSortedArrays(self, nums1, nums2):
        """
        寻找两个正序数组的中位数。
        时间复杂度: O(log(min(m, n)))
        空间复杂度: O(1)
        """
        if len(nums1) > len(nums2):
            nums1, nums2 = nums2, nums1

        m, n = len(nums1), len(nums2)
        low, high = 0, m

        while low <= high:
            partitionX = (low + high) // 2
            partitionY = (m + n + 1) // 2 - partitionX

            maxLeftX = nums1[partitionX - 1] if partitionX > 0 else float('-inf')
            minRightX = nums1[partitionX] if partitionX < m else float('inf')

            maxLeftY = nums2[partitionY - 1] if partitionY > 0 else float('-inf')
            minRightY = nums2[partitionY] if partitionY < n else float('inf')

            if maxLeftX <= minRightY and maxLeftY <= minRightX:
                if (m + n) % 2 == 0:
                    return (max(maxLeftX, maxLeftY) + min(minRightX, minRightY)) / 2.0  # 确保返回浮点数
                else:
                    return float(max(maxLeftX, maxLeftY)) # 确保返回浮点数
            elif maxLeftX > minRightY:
                high = partitionX - 1
            else:
                low = partitionX + 1

class Solution:
    def zhaozhongweihsu(self,nums1,nums2):
        if len(nums1)>len(nums1):
            nums1,nums2=nums2,nums1
        m,n=nums1,nums2
        low,high=0,m

        while low<high:
            partitionx=(low+high)//2
            partitiony=(m+n+1)//2-partitionx
            maxleftx=nums1[partitionx-1] if partitionx>0 else float('-inf')
            minrightx=nums1[partitionx] if partitionx<m else float('inf')

            maxlefty=nums2[partitiony-1]if partitiony>0 else float("-inf")
            minrighty=nums2[partitiony]if partitiony<n else float('inf')

            if maxleftx<=minrighty and maxlefty<=minrightx:
                if (m+n)%2==0:
                    return (max(maxleftx,maxlefty)+min(minrightx,minrighty))/2.0
                elif maxleftx>minrighty:
                    high=partitionx-1
                else:
                    low=partitionx+1






















class Solution:
    def findMedianSortedArrays(self,nums1,nums2):
        if len(nums1)>len(nums2):
            nums1,nums2=nums2,nums1
        m,n=len(nums1),len(nums2)
        low,high=0,m
        while low<high:
            partitionX=(low+high)//2
            partitionY=(m+n+1)//2-partitionX
            maxLeftX=nums1[partitionX-1] if partitionX>0 else float('-inf')
            minRightX=nums1[partitionX]if partitionX<m else float('inf')

            maxLeftY=nums2[partitionY-1]if partitionY>0else float('-inf')
            minRightY=nums2[partitionY]if partitionY<n else float('inf')
            if maxLeftX<=minRightY and maxLeftY <=minRightX:
                if(m+n)%2==0:
                    return (max(maxLeftX,maxLeftY))+min(minRightX,minRightY)/2.0#确保返回浮点数
                else:
                    return float(max(maxLeftX,maxLeftY))#确保返回浮点数
            elif maxLeftX>minRightY:
                high=partitionX-1
            else:
                low=partitionX+1

class Solution:
    def findmediansortedarrays(self,nums1,nums2):
        if len(nums1)>len(nums2):
            nums1,nums2=nums2,nums1
        m,n=len(nums1),len(nums2)
        low,high=0,m
        while low<high:
            partitionx=(low+high)//2
            partitiony=(m+n+1)//2-partitionx

            maxleftx=nums1[partitionx-1] if partitionx>0 else float('-inf')
            minrightx=nums1[partitiony] if partitiony<m else float('inf')

            maxlefty=nums2[partitiony-1]if partitiony>0 else float('-inf')
            minrighty=nums2[partitiony]if partitiony<n else float('inf')
            if maxleftx<=minrighty and maxlefty<=minrightx:
                if(m+n)%2==0:
                    return (max(maxleftx,maxlefty)+min(minrightx+minrighty))/2.0
                else:
                    return float(max(maxleftx,maxlefty))
            elif maxleftx>minrighty:
                high=partitionx-1
            else:
                low=partitionx+1


class Solution:
    def findmeiansoredarrays(self,nums1,nums2):
        if len(nums1)>len(nums2):
            nums1,nums2=nums2,nums1
        m,n=len(nums1),len(nums2)
        low,high=0,m
        while low<high:
            partitionx=(low+high)//2
            partitiony=(m+n+1)//2-partitionx

            maxleftx=nums1[partitionx-1] if partitionx>0 else float("-inf")
            minrightx=nums1[partitionx] if partitionx<m else float('inf')

            maxlefty=nums2[partitiony-1]if partitiony >0else float('inf')
            minrighty=nums2[partitiony]if partitiony<n else float('inf')

            if maxleftx<=minrighty and minrightx>=maxlefty:
                if (m+n)%2==0:
                    return (max(maxleftx,maxlefty)+min(minrightx,minrighty))
                else:
                    return (max(maxleftx,maxlefty))
            elif maxleftx>minrighty:
                high=partitionx-1
            else:
                low=partitionx+1

class Solution:
    def find(self,nums1,nums2):#定义一个功能为查找中位数的函数
        if len(nums1)>len(nums2):
            nums1,nums2=nums2,nums1#重新赋值，将个数少的赋值到nums1，方便后续计算
        m,n=len(nums1),len(nums2)
        low,high=0,m
        while low<high:
            partitionx=(low+high)//2
            partitiony=(m+n+1)//2-partitionx

            maxleftx=nums1[partitionx-1]if partitionx>0 else float('-inf')
            minrightx=nums1[partitionx]if partitionx<m else float('inf')

            maxlefty=nums2[partitiony-1]if partitiony>0else float('-inf')
            minrighty=nums2[partitiony]if partitiony<n else float('inf')

            if maxleftx<=minrighty and minrightx>=maxlefty:
                if (m+n)%2==0:
                    return (max(maxleftx,maxlefty)+min(minrightx,minrighty))
                else:
                    return (max(maxleftx,maxlefty))

            elif maxleftx>minrighty:
                high=partitionx-1

            else :
                low=partitionx+1

class Solution:
    def zhaozhongweishu(self,nums1,nums2):
        if len(nums1)>len(nums2):
            nums1,nums2=nums2,nums1
        m,n=len(nums1),len(nums2)
        low,high=0,m
        while low<high:
            partitionx=(low+high)//2
            partitiony=(m+n+1)//2-partitionx

            maxleftx=nums1[partitionx-1]if partitionx>0 else float('-inf')
            minrightx=nums1[partitionx]if partitionx<m else float('inf')

            maxlefty=nums2[partitiony-1]if partitiony>0else float('-inf')
            minrighty=nums2[partitiony]if partitiony<n else float('inf')

            if maxleftx<=minrighty and minrightx >=maxlefty:
                if(m+n)%2==0:
                    return (max(maxleftx,maxlefty)+min(minrightx,minrighty))
                elif maxleftx>minrighty:
                    high=partitionx-1
                else :
                    low=partitionx+1

class Solution:
    def zhaozhongweishu(self,nums1,nums2):
        if len(nums1)>len(nums2):
            nums1,nums2=nums2,nums1
        m,n=len(nums1),len(nums2)
        low,high=0,m
        while low<high:
            partitionx=(low+high)//2
            partitiony=(m+n+1)//2-partitionx

            maxleftx=nums1[partitionx-1]if partitionx>0 else float('-inf')
            minrightx=nums1[partitionx]if partitionx<m else float('inf')

            maxlefty=nums2[partitiony-1]if partitiony>0 else float('-inf')
            minrighty=nums2[partitiony]if partitiony <n else float('inf')

            if maxleftx<=minrighty and minrightx >=maxlefty:
                if (m+n)%2==0:
                    return (max(maxleftx,maxlefty)+min(minrightx,minrighty))/2.0#return (max(maxLeftX, maxLeftY) + min(minRightX, minRightY)) / 2.0  # 确保返回浮点数
                else :
                    return float(max(maxleftx,maxlefty))#确保返回浮点数
            elif maxleftx>minrighty:
                high=partitionx-1

            else:
                low=partitionx+1


class Solution:
    def zhaozhongweishu(self,nums1,nums2):
        if len(nums1)>len(nums2):
            nums1,nums2=nums2,nums1
        m,n=len(nums1),len(nums2)
        low,high=0,m
        while low<high:
            partitionx=(low+high)//2
            partitiony=(m+n+1)//2-partitionx

            maxleftx=nums1[partitionx-1] if partitionx>0 else float('-inf')
            minrightx=nums1[partitionx]if partitionx<m else float('inf')
            
            maxlefty=nums2[partitiony-1]if partitiony>0 else float('-inf')
            minrighty=nums2[partitiony]if partitiony<n else float('inf')

            if maxleftx<=minrighty and minrightx >=maxlefty:
                if(m+n)%2==0:
                    return (max(maxleftx,maxlefty)+min(minrightx,minrighty))/2.0
                else :
                    return max(maxleftx,maxlefty)
            elif maxleftx>minrighty:
                high=partitionx-1
            else :
                low=partitionx+1


class Solution:
    def zhaozhongweishu(self,nums1,nums2):
        if len(nums1)>len(nums2):
            nums1,nums2=nums2,nums1
        m,n=len(nums1),len(nums2)
        low,high=0,m
        while low<high:
            partitionx=(low+high)//2
            partitiony=(m+n+1)//2-partitionx

            maxleftx=nums1[partitionx-1] if partitionx>0 else float('-inf')
            minrightx=nums1[partitionx]if partitionx<m else float('inf')

            maxlefty=nums2[partitiony-1]if partitiony>0 else float('-inf')
            minrighty=nums2[partitiony]if partitiony<n else float('inf')

            if maxleftx<=minrighty and minrightx>=maxlefty:
                if (m+n)%2==0:
                    return (max(maxleftx,maxlefty)+min(minrightx,minrighty))/2.0
                else:
                    return max(maxleftx,maxlefty)
            elif maxleftx>minrighty:
                high=partitionx-1
            else:
                low=partitionx+1



class Solution:
    def zhaozhongweishu(self,nums1,nums2):
        if len(nums1)>len(nums2):
            nums1,nums2=nums2,nums1
        m,n=len(nums1),len(nums2)
        low,high=0,m
        while low <high:
            partitionx=(low+high)//2
            partitiony=(m+n)//2-partitionx

            maxleftx=nums1[partitionx-1] if partitionx>0 else float('-inf')
            minrightx=nums1[partitionx]if partitionx<m else float('inf')

            maxlefty=nums2[partitiony-1]if partitiony>0 else float('-inf')
            minrighty=nums2[partitiony]if partitiony<n else float('inf')


            if maxleftx<=minrightx and minrightx>=maxlefty:
                if (m+n)%2==0:
                    return (max(maxleftx,maxlefty)+min(minrightx,minrighty))/2.0
                else:
                    return max(maxleftx,maxlefty)
            elif maxleftx>minrighty:
                high=partitionx-1
            else:
                low=partitionx+1














# 示例测试
if __name__ == "__main__":
    sol = Solution()

    # 测试用例 1
    nums1 = [1, 3]
    nums2 = [2]
    print(f"中位数: {sol.findMedianSortedArrays(nums1, nums2)}")

    # 测试用例 2
    nums1 = [1, 2]
    nums2 = [3, 4]
    print(f"中位数: {sol.findMedianSortedArrays(nums1, nums2)}")

    # 测试用例 3 (空数组)
    nums1 = []
    nums2 = [1, 2, 3]
    print(f"中位数: {sol.findMedianSortedArrays(nums1, nums2)}")

    # 测试用例 4 (一个数组为空)
    nums1 = [1, 2]
    nums2 = []
    print(f"中位数: {sol.findMedianSortedArrays(nums1, nums2)}")
